<script setup name="User">
	import {ref, reactive} from 'vue'
	import {Search} from '@element-plus/icons-vue'
	import {ElMessage, ElMessageBox} from "element-plus";
	import {userApi} from "@/api/user/user.js";
	
	//表格数据
	let userList = ref([])
	const total = ref(0)
	
	//分页信息，搜索条件
	const userQuery = reactive({
		page: 1,
		limit: 10,
		name: '',
	})

	//加载数据
	const loadData = () => {
		userApi.list(userQuery).then(result => {
			userList.value = result.data.list
			total.value = result.data.total
		})
	}
	loadData()
	
	//搜索
	const onSearch = () => {
		userQuery.page = 1
		loadData()
	}
	
	//搜索重置按钮
	const originData = () => ({ name: ""});
	const resetForm = () => {
		Object.assign(userQuery, originData());
		loadData()
	};
	
	//修改账号status状态
	const handleSwitchChange = (row) => {
		userApi.updateStatus(row.id, row.status).then(result => {
			if (result.code === 0) {
				ElMessage.success(result.msg)
			} else {
				ElMessage.error(result.msg)
			}
		})
	}
</script>
<template>
	<div class="back">
		<el-card class="">
			<!--		搜索框-->
			<el-form :inline="true" :model="formInline" class="demo-form-inline">
				<el-form-item label="姓名：">
					<el-input v-model="userQuery.name" placeholder="请输入姓名" clearable/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" :icon="Search" @click="onSearch">搜索</el-button>
					<el-button @click="resetForm" round>重置</el-button>
				</el-form-item>
			</el-form>
			
			<!--		信息表格-->
			<el-table ref="multipleTableRef" :data="userList" border stripe style="width: 100%" height="450px">
				<el-table-column fixed prop="id" label="ID" sortable width="70" align="center"/>
				<el-table-column prop="name" label="姓名" align="center" min-width="120"/>
				<el-table-column prop="nickName" label="昵称" align="center" min-width="90"/>
				<el-table-column prop="email" label="邮箱" align="center" min-width="185"/>
				<el-table-column prop="phone" label="联系方式" align="center" min-width="130"/>
				<el-table-column prop="image" label="头像" align="center" min-width="150" show-overflow-tooltip>
					<template #default="scope">
						<img :src="scope.row.image" style="max-height: 40px;max-width: 120px"/>
					</template>
				</el-table-column>
				<el-table-column prop="status" label="状态" align="center" min-width="120">
					<template #default="scope">
						<el-switch
							v-model="scope.row.status"
							class="ml-2"
							inline-prompt
							size="large"
							style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
							active-text="正常"
							:active-value="1"
							inactive-text="停用"
							:inactive-value="0"
							@change="handleSwitchChange(scope.row)"
						/>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="创建时间" align="center" min-width="210"/>
			</el-table>
			<el-pagination
				v-model:current-page="userQuery.page"
				v-model:page-size="userQuery.limit"
				:page-sizes="[5, 10, 15, 20]"
				:size="size"
				:disabled="disabled"
				:background="background"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
				@change="loadData"
				style="margin-top: 20px; justify-content: flex-end"
			/>
		</el-card>
	</div>

</template>

<style scoped>
	.avatar-uploader .avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
<style>
	.avatar-uploader .el-upload {
		border: 1px dashed var(--el-border-color);
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
		transition: var(--el-transition-duration-fast);
	}
	
	.avatar-uploader .el-upload:hover {
		border-color: var(--el-color-primary);
	}
	
	.el-icon.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		text-align: center;
	}
</style>